/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sddengine;

import java.awt.image.BufferedImage;
import java.awt.Graphics;

/**
 *
 * @author Jøgge
 */
abstract public class AnimatedSprite extends Sprite
{
    
    protected BufferedImage[] images;
    
    int status;
    int direction;
    int currentFrame;
    int from;
    int to;
    int animationTime;
    int runTime;
    int oldStat;
    int oldDir;
    int oldX;
    int oldY;
    
    double horizSpeed;
    double vertSpeed;
    
    
    public AnimatedSprite(BufferedImage[] images, int animationTime)
    {
        this.animationTime = animationTime;
        this.images = images;   
    }
    public AnimatedSprite(BufferedImage[] images,int animationTime, int x, int y)
    {
        this.animationTime = animationTime;
        this.images = images;
        this.x = x;
        this.y = y;
        this.width = images[0].getWidth();
        this.height = images[0].getHeight();
        
        
    }
       
    
    @Override
    public void update() 
    {	
        //System.out.println(currentFrame);
        runTime++;
        if(runTime >= (animationTime/10))
        {
			if (currentFrame < to) 
			{
				currentFrame++;
				runTime = 0;
				
			} 
			else 
			{
				currentFrame = from;
				

			}
        }  
    }

    @Override
    public void draw(Graphics g) 
    {
        g.drawImage(images[currentFrame], x, y, null);
    }
    
    abstract protected void animationChanged(int oldStat, int oldDir, int status, int direction);
   
    
    public void setAnimation(int status, int direction)
    {
        this.oldStat = this.status;
        this.oldDir = this.direction;
        this.status = status;
        this.direction = direction;
        
        animationChanged(oldStat, oldDir, status, direction);
    }
    
    public void setAnimationFrames(int from, int to)
    {
        this.from = from;
        this.to = to;
       
            if(this.oldDir != this.direction)  {
                
                runTime = (animationTime/10);
                currentFrame = from;
                
            }
        
    }
    
    
    public void setAnimationTime(int animationTime)
    {
        this.animationTime = animationTime;
    }
    
    public int getDirection()
    {
        return direction;
    }
    
    public void setHorizontalSpeed(double speed)
    {
    	this.oldX = x;
        this.horizSpeed = speed;
        
        this.setX(x+=horizSpeed);
    }
    
    public void setVerticalSpeed(double speed)
    {
    	this.oldY = y;
        this.vertSpeed = speed;
        
        this.setY(y+=vertSpeed);
    }
   
     
}
